package com.spark.lola.hander;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.spark.lola.feign.LogisticsBiFeignService;
import com.spark.lola.logistics.service.LogisticsOrderInfoService;
import com.spark.lola.logistics.vo.OrderCountStatisticVo;
import com.spark.lola.logistics.vo.OrderStatisticVo;
import com.spark.lola.statistics.vo.PurchaseStatisticsVo;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.netty.annotation.HawkBean;
import com.spark.lola.support.netty.annotation.HawkMethod;
import com.spark.lola.support.netty.api.NettyPermissionService;
import com.spark.lola.support.netty.constant.NettyCommands;
import com.spark.lola.support.wms.cache.CollectionCache;
import com.spark.lola.support.wms.constant.WmsNettyCommands;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.InetSocketAddress;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/06/16 18:04
 **/
@HawkBean
public class LogisticsHandler {

    private static final Logger logger = LoggerFactory.getLogger(LogisticsHandler.class);

    @Autowired
    NettyPermissionService nettyPermissionService;

    @Autowired
    private LogisticsOrderInfoService logisticsOrderInfoService;

    /**
     * 查询货运概况
     */
    @HawkMethod(cmd = WmsNettyCommands.QUERY_LOGISTICS_STATISTICS, version = NettyCommands.COMMANDS_VERSION)
    public String queryCount(long seqId, byte[] body, ChannelHandlerContext ctx) throws ParseException {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // 获取时间类型
        String json = new String(body);
        JSONObject request = JSON.parseObject(json);
        String timeType = request.getString("timeType");
        String channelId = ctx.channel().id().asLongText();
        // 获取用户ID
        String userId = nettyPermissionService.getUserId(channelId);
        logger.info("管理员【{}】通道【{}】参数【{}】请求物流概况", userId, channelId, timeType);
        OrderCountStatisticVo vo = logisticsOrderInfoService.countStatistic(timeType);
        return JSON.toJSONString(ResponseBean.success(vo));
    }

    /**
     * 查询货运概况-自营
     */
    @HawkMethod(cmd = WmsNettyCommands.QUERY_LOGISTICS_SELF, version = NettyCommands.COMMANDS_VERSION)
    public String querySelf(long seqId, byte[] body, ChannelHandlerContext ctx) throws ParseException {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // 获取时间类型
        String json = new String(body);
        JSONObject request = JSON.parseObject(json);
        Date startDate = request.getDate("startDate");
        Date endDate = request.getDate("endDate");
        String channelId = ctx.channel().id().asLongText();
        // 获取用户ID
        String userId = nettyPermissionService.getUserId(channelId);
        logger.info("管理员【{}】通道【{}】参数【{}，{}】请求物流自营车辆概况", userId, channelId, startDate, new Date());
        List<OrderStatisticVo> list = logisticsOrderInfoService.queryDate("self", startDate, new Date());
        return JSON.toJSONString(ResponseBean.success(list));
    }

    /**
     * 查询货运概况-外调
     */
    @HawkMethod(cmd = WmsNettyCommands.QUERY_LOGISTICS_OTHER, version = NettyCommands.COMMANDS_VERSION)
    public String queryOther(long seqId, byte[] body, ChannelHandlerContext ctx) throws ParseException {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // 获取时间类型
        String json = new String(body);
        JSONObject request = JSON.parseObject(json);
        Date startDate = request.getDate("startDate");
        Date endDate = request.getDate("endDate");
        String channelId = ctx.channel().id().asLongText();
        // 获取用户ID
        String userId = nettyPermissionService.getUserId(channelId);
        logger.info("管理员【{}】通道【{}】参数【{}，{}】请求物流外调车辆概况", userId, channelId, startDate, new Date());
        List<OrderStatisticVo> list = logisticsOrderInfoService.queryDate("other", startDate, new Date());
        return JSON.toJSONString(ResponseBean.success(list));
    }

    /**
     * 查询货运概况-回头
     */
    @HawkMethod(cmd = WmsNettyCommands.QUERY_LOGISTICS_RETURN, version = NettyCommands.COMMANDS_VERSION)
    public String queryReturn(long seqId, byte[] body, ChannelHandlerContext ctx) throws ParseException {
        InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        // 获取时间类型
        String json = new String(body);
        JSONObject request = JSON.parseObject(json);
        Date startDate = request.getDate("startDate");
        Date endDate = request.getDate("endDate");
        String channelId = ctx.channel().id().asLongText();
        // 获取用户ID
        String userId = nettyPermissionService.getUserId(channelId);
        logger.info("管理员【{}】通道【{}】参数【{}，{}】请求物流回头车辆概况", userId, channelId, startDate, new Date());
        List<OrderStatisticVo> list = logisticsOrderInfoService.queryDate("return", startDate, new Date());
        return JSON.toJSONString(ResponseBean.success(list));
    }

}
